package core.applications.communication.messaging;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;

import net.sourceforge.peertwitter.core.applications.communication.messaging.ReliableMessageEnvelope;

import org.junit.Test;

import rice.p2p.commonapi.Message;


public class ReliableMessageEnvelopeTest {


	
	@Test
	public void anEnvelopeContainsTheProperMessagePassedOnItsConstruction() {
		Message aMessage = new Message() {

			private static final long serialVersionUID = 1L;

			@Override
			public int getPriority() {
				return 0;
			}
			
		};
		ReliableMessageEnvelope envelope = new ReliableMessageEnvelope(aMessage);
		assertEquals(aMessage, envelope.getMessage());
	}
	
	
	@Test
	public void anEnvelopeHasMediumHighPriorityWhenTheMessageItCarriesHasLowerPriorityThanIt() {
		Message aMessage = new Message() {

			private static final long serialVersionUID = 1L;

			@Override
			public int getPriority() {
				return Message.MEDIUM_HIGH_PRIORITY;
			}
			
		};
		ReliableMessageEnvelope envelope = new ReliableMessageEnvelope(aMessage);
		assertEquals(Message.MEDIUM_HIGH_PRIORITY, envelope.getPriority());
	}
	
	@Test
	public void anEnvelopeAssumesTheMessageItCarriesPriorityWhenItIsGreaterThanMediumHigh() {
		Message highPriorityMsg = new Message() {

			/**
			 * 
			 */
			private static final long serialVersionUID = 1L;

			@Override
			public int getPriority() {
				return Message.HIGH_PRIORITY;
			}
			
		};
		
		Message maxPriorityMessage = new Message() {

			private static final long serialVersionUID = 1L;

			@Override
			public int getPriority() {
				return Message.MAX_PRIORITY;
			}
			
		};

		ReliableMessageEnvelope envelopeHigh = new ReliableMessageEnvelope(highPriorityMsg);
		ReliableMessageEnvelope envelopeMax = new ReliableMessageEnvelope(maxPriorityMessage);
		
		assertEquals(Message.HIGH_PRIORITY, envelopeHigh.getPriority());
		assertEquals(Message.MAX_PRIORITY, envelopeMax.getPriority());
	}
	
	@Test
	public void twoEnvelopsAreEqualWhenTheyCarryTheSameMessage() {
		Message messageOne = new Message() {
			
			/**
			 * 
			 */
			private static final long serialVersionUID = 9113021256517100241L;

			@Override
			public int getPriority() {
				return 0;
			}
		};
		
	Message messageTwo = new Message() {
			
			/**
		 * 
		 */
		private static final long serialVersionUID = 306224617107137561L;

			@Override
			public int getPriority() {
				return 0;
			}
		};
		ReliableMessageEnvelope aEnvelopeWithMessageOne = new ReliableMessageEnvelope(messageOne);
		ReliableMessageEnvelope anotherEnvelopeWithMessageOne = new ReliableMessageEnvelope(messageOne);
		
		assertEquals(aEnvelopeWithMessageOne, anotherEnvelopeWithMessageOne);
		ReliableMessageEnvelope aEnvelopeWithMessageTwo = new ReliableMessageEnvelope(messageTwo);
		
		assertFalse(aEnvelopeWithMessageOne.equals(aEnvelopeWithMessageTwo));
		assertFalse(anotherEnvelopeWithMessageOne.equals(aEnvelopeWithMessageTwo));
	}
}

